﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DbLinq.Oracle;
using DbLinq.Linq;
using CMISLibrary;
using ObjectDanhMuc;
using ObjectDanhMuc.DB;

namespace busDanhMuc
{
    public class cls_D_DVI_QLY
    {
        #region   Atributes

        private cls_D_DVI_QLY_Controller obj_D_DVI_QLY_Controller;
        private D_DVI_QLY obj_D_DVI_QLY_Info;

        #endregion


        #region   Constructor


        public cls_D_DVI_QLY()
        {
            obj_D_DVI_QLY_Info = new D_DVI_QLY();
            obj_D_DVI_QLY_Controller = new cls_D_DVI_QLY_Controller();
        }
        public cls_D_DVI_QLY(DataSet entity)
        {
            obj_D_DVI_QLY_Info = new D_DVI_QLY();
            obj_D_DVI_QLY_Controller = new cls_D_DVI_QLY_Controller();
        }

        #endregion


        #region Method

        public string heloo()
        {
            return "halu";
        }


        public DataSet select_D_DVI_QLY_captren(string strMadviqly)
        {
            try
            {
                var a = obj_D_DVI_QLY_Controller.select_D_DVI_QLY_captren(strMadviqly);
                if (a == null)
                {
                    return null;
                }

                var q = from p in a
                        select new
                        {
                            p.MA_DVIQLY,
                            p.TEN_DVIQLY,
                            p.MA_DVICTREN,
                            p.CAP_DVI,
                            p.DIA_CHI,
                            p.ID_DIA_CHINH,
                            p.DIEN_THOAI,
                            p.DTHOAI_KDOANH,
                            p.DTHOAI_NONG,
                            p.DTHOAI_TRUC,
                            p.FAX,
                            p.EMAIL,
                            p.MA_STHUE,
                            p.DAI_DIEN,
                            p.CHUC_VU,
                            p.SO_UQUYEN,
                            p.NGAY_UQUYEN,
                            p.TEN_DVIUQ,
                            p.DCHI_DVIUQ,
                            p.CVU_UQUYEN,
                            p.TNGUOI_UQUYEN,
                            p.TEN_TINH,
                            p.WEBSITE,
                        };

                if (q != null && q.Count() > 0)
                {
                    DataTable dt = Utility.LINQToDataTable(q);
                    dt.TableName = "D_DVI_QLY";
                    dt.AcceptChanges();
                    DataSet ds = new DataSet();
                    ds.Tables.Add(dt);
                    return ds;
                }
                else
                {
                    return null;
                }

            }
            catch
            {
                return null;
            }
        }

        public DataSet get_SQUAN_TRI(string strmadvi, ref  string strErr)
        {
            DataSet ds_SQUAN_TRI = new DataSet();
            DataTable dt_SQUAN_TRI = new DataTable();

            try
            {
                dt_SQUAN_TRI = obj_D_DVI_QLY_Controller.select_SQUAN_TRI(strmadvi, ref strErr);
                if (dt_SQUAN_TRI.Rows.Count > 0)
                {

                    ds_SQUAN_TRI.Tables.Add(dt_SQUAN_TRI);
                    return ds_SQUAN_TRI;
                }
                else return null;



            }
            catch
            { return null; }
        }

        public DataSet get_D_DVIQLY(string strmadvi)
        {
            DataSet ds_DMDV = new DataSet();
            DataTable dt_DMDV = new DataTable();
            try
            {
                ds_DMDV = obj_D_DVI_QLY_Controller.select_D_DVI_QLY(strmadvi);
                return ds_DMDV;
            }
            catch
            { return null; }
        }

        public DataSet select_D_DVI_QLY_All()
        {
            try
            {
                var a = obj_D_DVI_QLY_Controller.select_D_DVI_QLY_All();
                if (a == null)
                {
                    return null;
                }

                var q = from p in a
                        select new
                        {
                            p.MA_DVIQLY,
                            p.TEN_DVIQLY,
                            p.MA_DVICTREN,
                            p.CAP_DVI,
                            p.DIA_CHI,
                            p.ID_DIA_CHINH,
                            p.DIEN_THOAI,
                            p.DTHOAI_KDOANH,
                            p.DTHOAI_NONG,
                            p.DTHOAI_TRUC,
                            p.FAX,
                            p.EMAIL,
                            p.MA_STHUE,
                            p.DAI_DIEN,
                            p.CHUC_VU,
                            p.SO_UQUYEN,
                            p.NGAY_UQUYEN,
                            p.TEN_DVIUQ,
                            p.DCHI_DVIUQ,
                            p.CVU_UQUYEN,
                            p.TNGUOI_UQUYEN,
                            p.TEN_TINH,
                            p.WEBSITE,
                        };

                if (q != null && q.Count() > 0)
                {
                    DataTable dt = Utility.LINQToDataTable(q);
                    dt.TableName = "D_DVI_QLY";
                    dt.AcceptChanges();
                    DataSet ds = new DataSet();
                    ds.Tables.Add(dt);
                    return ds;
                }
                else
                {
                    return null;
                }

            }
            catch
            {
                return null;
            }
        }

        public DataSet select_D_DVI_QLY_ALL_cbo()
        {
            var D_DVI_QLY = obj_D_DVI_QLY_Controller.select_D_DVI_QLY_All().ToList();
            try
            {
                var q = (from p in D_DVI_QLY
                         select new
                         {
                             p.MA_DVIQLY,
                             p.TEN_DVIQLY,
                             SEARCH = p.MA_DVIQLY + " - " + p.TEN_DVIQLY
                         }).ToList();
                if (q != null && q.Count() > 0)
                {
                    DataTable dt = Utility.LINQToDataTable(q);
                    DataSet ds = new DataSet();
                    dt.TableName = "D_DVI_QLY";
                    dt.AcceptChanges();
                    ds.Tables.Add(dt);
                    return ds;
                }
                else return null;
            }
            catch
            {
                return null;
            }
        }

        public DataSet select_D_DVI_QLY_Hight_cbo(int capdv)
        {
            var D_DVI_QLY = obj_D_DVI_QLY_Controller.select_D_DVI_QLY_All().ToList();
            try
            {
                var q = (from p in D_DVI_QLY
                         where (p.CAP_DVI < capdv) || (p.CAP_DVI == 0)
                         select new
                         {
                             p.MA_DVIQLY,
                             p.TEN_DVIQLY,
                             SEARCH = p.MA_DVIQLY + " - " + p.TEN_DVIQLY
                         }).ToList();
                if (q != null && q.Count() > 0)
                {
                    DataTable dt = Utility.LINQToDataTable(q);
                    DataSet ds = new DataSet();
                    dt.TableName = "D_DVI_QLY";
                    dt.AcceptChanges();
                    ds.Tables.Add(dt);
                    return ds;
                }
                else return null;
            }
            catch
            {
                return null;
            }
        }

        public DataSet select_D_DVI_QLY_Like(string strMadviqly)
        {
            var a = obj_D_DVI_QLY_Controller.select_D_DVI_QLY_Like(strMadviqly);
            try
            {
                if (a == null)
                {
                    return null;
                }

                var q = from p in a
                        select new
                        {
                            p.MA_DVIQLY,
                            p.TEN_DVIQLY,
                            p.MA_DVICTREN,
                            p.CAP_DVI,
                            p.DIA_CHI,
                            p.ID_DIA_CHINH,
                            p.DIEN_THOAI,
                            p.DTHOAI_KDOANH,
                            p.DTHOAI_NONG,
                            p.DTHOAI_TRUC,
                            p.FAX,
                            p.EMAIL,
                            p.MA_STHUE,
                            p.DAI_DIEN,
                            p.CHUC_VU,
                            p.SO_UQUYEN,
                            p.NGAY_UQUYEN,
                            p.TEN_DVIUQ,
                            p.DCHI_DVIUQ,
                            p.CVU_UQUYEN,
                            p.TNGUOI_UQUYEN,
                            p.TEN_TINH,
                            p.WEBSITE,
                        };

                if (q != null && q.Count() > 0)
                {
                    DataTable dt = Utility.LINQToDataTable(q);
                    dt.TableName = "D_DVI_QLY";
                    dt.AcceptChanges();
                    DataSet ds = new DataSet();
                    ds.Tables.Add(dt);
                    return ds;
                }
                else
                {
                    return null;
                }
            }
            catch
            {
                return null;
            }
        }

        public DataSet select_D_DVI_QLY_ID(string strMaDviqly)
        {
            try
            {
                var a = obj_D_DVI_QLY_Controller.select_D_DVI_QLY_ID(strMaDviqly);
                if (a == null)
                {
                    return null;
                }
                //else
                //{
                //    DataTable dt = Utility.LINQToDataTable(a);
                //    dt.TableName = "IN_BCKD";
                //    DataSet ds = new DataSet();
                //    ds.Tables.Add(dt);
                //    return ds;
                //}

                var q = from p in a
                        select new
                        {
                            p.MA_DVIQLY,
                            p.TEN_DVIQLY,
                            p.MA_DVICTREN,
                            p.CAP_DVI,
                            p.DIA_CHI,
                            p.ID_DIA_CHINH,
                            p.DIEN_THOAI,
                            p.DTHOAI_KDOANH,
                            p.DTHOAI_NONG,
                            p.DTHOAI_TRUC,
                            p.FAX,
                            p.EMAIL,
                            p.MA_STHUE,
                            p.DAI_DIEN,
                            p.CHUC_VU,
                            p.SO_UQUYEN,
                            p.NGAY_UQUYEN,
                            p.TEN_DVIUQ,
                            p.DCHI_DVIUQ,
                            p.CVU_UQUYEN,
                            p.TNGUOI_UQUYEN,
                            p.TEN_TINH,
                            p.WEBSITE,
                        };

                if (q != null && q.Count() > 0)
                {
                    DataTable dt = Utility.LINQToDataTable(q);
                    dt.TableName = "D_DVI_QLY";
                    dt.AcceptChanges();
                    DataSet ds = new DataSet();
                    ds.Tables.Add(dt);
                    return ds;
                }
                else
                {
                    return null;
                }
            }
            catch
            {
                return null;
            }
        }

        

        public DataSet select_D_DVI_QLY_IS_Captren(string strMaDviqly)
        {
            try
            {
                var a = obj_D_DVI_QLY_Controller.select_D_DVI_QLY_IS_Captren(strMaDviqly);
                if (a == null)
                {
                    return null;
                }
                //else
                //{
                //    DataTable dt = Utility.LINQToDataTable(a);
                //    dt.TableName = "IN_BCKD";
                //    DataSet ds = new DataSet();
                //    ds.Tables.Add(dt);
                //    return ds;
                //}

                var q = from p in a
                        select new
                        {
                            p.MA_DVIQLY,
                            p.TEN_DVIQLY,
                            p.MA_DVICTREN,
                            p.CAP_DVI,
                            p.DIA_CHI,
                            p.ID_DIA_CHINH,
                            p.DIEN_THOAI,
                            p.DTHOAI_KDOANH,
                            p.DTHOAI_NONG,
                            p.DTHOAI_TRUC,
                            p.FAX,
                            p.EMAIL,
                            p.MA_STHUE,
                            p.DAI_DIEN,
                            p.CHUC_VU,
                            p.SO_UQUYEN,
                            p.NGAY_UQUYEN,
                            p.TEN_DVIUQ,
                            p.DCHI_DVIUQ,
                            p.CVU_UQUYEN,
                            p.TNGUOI_UQUYEN,
                            p.TEN_TINH,
                            p.WEBSITE,
                        };

                if (q != null && q.Count() > 0)
                {
                    DataTable dt = Utility.LINQToDataTable(q);
                    dt.TableName = "D_DVI_QLY";
                    dt.AcceptChanges();
                    DataSet ds = new DataSet();
                    ds.Tables.Add(dt);
                    return ds;
                }
                else
                {
                    return null;
                }
            }
            catch
            {
                return null;
            }
        }

        public DataSet select_D_DVI_QLY_by_1_Col(DataSet Entity)
        {
            try
            {
                this.mapEntityToInfo(Entity);
                var q = from p in obj_D_DVI_QLY_Controller.select_D_DVI_QLY_by_1_Col()
                        select new
                        {
                            p.MA_DVIQLY,
                            p.TEN_DVIQLY,
                            p.MA_DVICTREN,
                            p.CAP_DVI,
                            p.DIA_CHI,
                            p.ID_DIA_CHINH,
                            p.DIEN_THOAI,
                            p.DTHOAI_KDOANH,
                            p.DTHOAI_NONG,
                            p.DTHOAI_TRUC,
                            p.FAX,
                            p.EMAIL,
                            p.MA_STHUE,
                            p.DAI_DIEN,
                            p.CHUC_VU,
                            p.SO_UQUYEN,
                            p.NGAY_UQUYEN,
                            p.TEN_DVIUQ,
                            p.DCHI_DVIUQ,
                            p.CVU_UQUYEN,
                            p.TNGUOI_UQUYEN,
                            p.TEN_TINH,
                            p.WEBSITE,
                        };
                if (q != null && q.Count() > 0)
                {
                    DataTable dt = Utility.LINQToDataTable(q);
                    dt.TableName = "D_DVI_QLY";
                    DataSet ds = new DataSet();
                    ds.Tables.Add(dt);
                    return ds;
                }
                else return null;
            }
            catch
            {
                return null;
            }

        }


        public DataSet select_D_DVI_QLY_by_Find(string strMaDviqly, DataSet Entity)
        {
            try
            {
                this.mapEntityToInfo(Entity);
                var q = from p in obj_D_DVI_QLY_Controller.select_D_DVI_QLY_by_1_Col()
                        orderby p.MA_DVIQLY, p.TEN_DVIQLY ascending
                        where (p.MA_DVIQLY.Contains(strMaDviqly))
                        select new
                        {
                            p.MA_DVIQLY,
                            p.TEN_DVIQLY,
                            p.MA_DVICTREN,
                            p.CAP_DVI,
                            p.DIA_CHI,
                            p.ID_DIA_CHINH,
                            p.DIEN_THOAI,
                            p.DTHOAI_KDOANH,
                            p.DTHOAI_NONG,
                            p.DTHOAI_TRUC,
                            p.FAX,
                            p.EMAIL,
                            p.MA_STHUE,
                            p.DAI_DIEN,
                            p.CHUC_VU,
                            p.SO_UQUYEN,
                            p.NGAY_UQUYEN,
                            p.TEN_DVIUQ,
                            p.DCHI_DVIUQ,
                            p.CVU_UQUYEN,
                            p.TNGUOI_UQUYEN,
                            p.TEN_TINH,
                            p.WEBSITE,
                        };
                if (q != null && q.Count() > 0)
                {
                    DataTable dt = Utility.LINQToDataTable(q);
                    dt.TableName = "D_DVI_QLY";
                    DataSet ds = new DataSet();
                    ds.Tables.Add(dt);
                    //return ds;

                    //DataRow[] adr = ds.Tables["D_DVI_QLY"].Select("1=1");
                    //foreach (DataRow dr in adr)

                    //============= tìm đơn vị cấp trên của các đơn vị  =============
                    string madvctss = "";
                    for (int i = 0; i < ds.Tables["D_DVI_QLY"].Rows.Count; i++)
                    {
                        string madvct = ds.Tables["D_DVI_QLY"].Rows[i]["MA_DVICTREN"].ToString();
                        if (madvct != madvctss)
                        {
                            var m = from p in obj_D_DVI_QLY_Controller.select_D_DVI_QLY_ID(madvct)
                                    select new
                                    {
                                        p.MA_DVIQLY,
                                        p.TEN_DVIQLY,
                                        p.MA_DVICTREN,
                                        p.CAP_DVI,
                                        p.DIA_CHI,
                                        p.ID_DIA_CHINH,
                                        p.DIEN_THOAI,
                                        p.DTHOAI_KDOANH,
                                        p.DTHOAI_NONG,
                                        p.DTHOAI_TRUC,
                                        p.FAX,
                                        p.EMAIL,
                                        p.MA_STHUE,
                                        p.DAI_DIEN,
                                        p.CHUC_VU,
                                        p.SO_UQUYEN,
                                        p.NGAY_UQUYEN,
                                        p.TEN_DVIUQ,
                                        p.DCHI_DVIUQ,
                                        p.CVU_UQUYEN,
                                        p.TNGUOI_UQUYEN,
                                        p.TEN_TINH,
                                        p.WEBSITE,
                                    };
                            if (m != null && m.Count() > 0)
                            {
                                DataTable dtt = Utility.LINQToDataTable(m);
                                string madviqly = dtt.Rows[0]["MA_DVIQLY"].ToString();
                                if (ds.Tables[0].Select("MA_DVIQLY = '" + madviqly + "'").Count() < 1)
                                {
                                    ds.Tables["D_DVI_QLY"].ImportRow(dtt.Rows[0]);
                                    ds.AcceptChanges();
                                }
                                //dtt.TableName = "D_DVI_QLY";
                                //DataSet ds = new DataSet();
                                //ds.Tables.Add(dt);
                                //return ds;
                            }
                        }
                        madvctss = madvct;
                    } // frea

                    return ds;

                }
                else return null;
            }
            catch
            {
                return null;
            }

        }


        public bool insert_D_DVI_QLY(DataSet Entity, ref string strErr)
        {
            try
            {
                this.mapEntityToInfo(Entity);
                if (obj_D_DVI_QLY_Controller == null)
                {
                    obj_D_DVI_QLY_Controller = new cls_D_DVI_QLY_Controller();
                }
                return obj_D_DVI_QLY_Controller.insert_D_DVI_QLY();
            }
            catch (Exception ex)
            {
                strErr = ex.Message;
                return false;
            }
        }


        public bool update_D_DVI_QLY(DataSet Entity, ref string strErr)
        {
            try
            {
                this.mapEntityToInfo(Entity);
                if (obj_D_DVI_QLY_Controller == null)
                {
                    obj_D_DVI_QLY_Controller = new cls_D_DVI_QLY_Controller();
                }
                return this.obj_D_DVI_QLY_Controller.update_D_DVI_QLY();
            }
            catch (Exception ex)
            {
                strErr = ex.Message;
                return false;
            }

        }

        public bool delete_D_DVI_QLY(DataSet Entity, ref string strErr)
        {
            try
            {
                this.mapEntityToInfo(Entity);
                if (obj_D_DVI_QLY_Controller == null)
                {
                    obj_D_DVI_QLY_Controller = new cls_D_DVI_QLY_Controller();
                }
                return this.obj_D_DVI_QLY_Controller.delete_D_DVI_QLY(ref strErr);
            }
            catch (Exception ex)
            {
                strErr = ex.Message;
                return false;
            }
        }


        public void mapEntityToInfo(DataSet Entity)
        {
            if (this.obj_D_DVI_QLY_Controller == null)
            {
                this.obj_D_DVI_QLY_Controller = new cls_D_DVI_QLY_Controller();
            }
            if (Entity.Tables[0].Rows[0]["MA_DVIQLY"].ToString() != string.Empty)
            {
                this.obj_D_DVI_QLY_Controller.pInfor.MA_DVIQLY = (Entity.Tables[0].Rows[0]["MA_DVIQLY"].ToString());
            }
            this.obj_D_DVI_QLY_Controller.pInfor.TEN_DVIQLY = (Entity.Tables[0].Rows[0]["TEN_DVIQLY"].ToString());
            this.obj_D_DVI_QLY_Controller.pInfor.MA_DVICTREN = (Entity.Tables[0].Rows[0]["MA_DVICTREN"].ToString());
            this.obj_D_DVI_QLY_Controller.pInfor.CAP_DVI = short.Parse(Entity.Tables[0].Rows[0]["CAP_DVI"].ToString());
            this.obj_D_DVI_QLY_Controller.pInfor.DIA_CHI = (Entity.Tables[0].Rows[0]["DIA_CHI"].ToString());
            if (Entity.Tables[0].Rows[0]["ID_DIA_CHINH"].ToString() != string.Empty)
            {
                this.obj_D_DVI_QLY_Controller.pInfor.ID_DIA_CHINH = long.Parse(Entity.Tables[0].Rows[0]["ID_DIA_CHINH"].ToString());
            }
            else
            {
                this.obj_D_DVI_QLY_Controller.pInfor.ID_DIA_CHINH = -1;
            }
            this.obj_D_DVI_QLY_Controller.pInfor.DIEN_THOAI = (Entity.Tables[0].Rows[0]["DIEN_THOAI"].ToString());
            this.obj_D_DVI_QLY_Controller.pInfor.DTHOAI_KDOANH = (Entity.Tables[0].Rows[0]["DTHOAI_KDOANH"].ToString());
            this.obj_D_DVI_QLY_Controller.pInfor.DTHOAI_NONG = (Entity.Tables[0].Rows[0]["DTHOAI_NONG"].ToString());
            this.obj_D_DVI_QLY_Controller.pInfor.DTHOAI_TRUC = (Entity.Tables[0].Rows[0]["DTHOAI_TRUC"].ToString());
            this.obj_D_DVI_QLY_Controller.pInfor.FAX = (Entity.Tables[0].Rows[0]["FAX"].ToString());
            this.obj_D_DVI_QLY_Controller.pInfor.EMAIL = (Entity.Tables[0].Rows[0]["EMAIL"].ToString());
            this.obj_D_DVI_QLY_Controller.pInfor.MA_STHUE = (Entity.Tables[0].Rows[0]["MA_STHUE"].ToString());
            this.obj_D_DVI_QLY_Controller.pInfor.DAI_DIEN = (Entity.Tables[0].Rows[0]["DAI_DIEN"].ToString());
            this.obj_D_DVI_QLY_Controller.pInfor.CHUC_VU = (Entity.Tables[0].Rows[0]["CHUC_VU"].ToString());
            this.obj_D_DVI_QLY_Controller.pInfor.SO_UQUYEN = (Entity.Tables[0].Rows[0]["SO_UQUYEN"].ToString());
            this.obj_D_DVI_QLY_Controller.pInfor.NGAY_UQUYEN = DateTime.Parse(Entity.Tables[0].Rows[0]["NGAY_UQUYEN"].ToString());
            this.obj_D_DVI_QLY_Controller.pInfor.TEN_DVIUQ = (Entity.Tables[0].Rows[0]["TEN_DVIUQ"].ToString());
            this.obj_D_DVI_QLY_Controller.pInfor.DCHI_DVIUQ = (Entity.Tables[0].Rows[0]["DCHI_DVIUQ"].ToString());
            this.obj_D_DVI_QLY_Controller.pInfor.CVU_UQUYEN = (Entity.Tables[0].Rows[0]["CVU_UQUYEN"].ToString());
            this.obj_D_DVI_QLY_Controller.pInfor.TNGUOI_UQUYEN = (Entity.Tables[0].Rows[0]["TNGUOI_UQUYEN"].ToString());
            this.obj_D_DVI_QLY_Controller.pInfor.TEN_TINH = (Entity.Tables[0].Rows[0]["TEN_TINH"].ToString());
            this.obj_D_DVI_QLY_Controller.pInfor.WEBSITE = (Entity.Tables[0].Rows[0]["WEBSITE"].ToString());


        }

        #endregion
    }
}